/*!
\copyright  Copyright (c) 2023 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief    Utility functions for the LE Audio Client
*/

#if defined(INCLUDE_LE_AUDIO_UNICAST_SOURCE) || defined(INCLUDE_LE_AUDIO_BROADCAST_SOURCE)
#include "le_audio_client_context.h"

uint16 leAudioClient_GetFrameDuration(bool is_source, le_audio_client_mode_t mode, uint8 stream_type)
{
    uint16 frame_duration = 0;
    uint8 bap_frame_duration = 0;
    le_audio_client_context_t* client_ctxt = leAudioClient_GetContext();

    if (mode == LE_AUDIO_CLIENT_MODE_UNICAST)
    {
        bap_frame_duration = is_source ? client_ctxt->session_data.codec_qos_config.srcFrameDuaration
                                       : client_ctxt->session_data.codec_qos_config.sinkFrameDuaration;
    }
#ifdef INCLUDE_LE_AUDIO_BROADCAST_SOURCE
    else
    {
        bap_frame_duration = client_ctxt->broadcast_session_data.frame_duration;
    }
#endif

#ifndef INCLUDE_LE_AUDIO_GAMING_MODE_APTX_LITE
    UNUSED(stream_type);
#endif

#ifdef INCLUDE_LE_AUDIO_GAMING_MODE_APTX_LITE
    if (mode == LE_AUDIO_CLIENT_MODE_UNICAST && leAudioClient_isVSAptxLite(client_ctxt->session_data.codec_qos_config))
    {
        return (stream_type == KYMERA_LE_STREAM_DUAL_MONO ? LE_APTX_LITE_FRAME_DURATION_6P25 : LE_APTX_LITE_DEFAULT_FRAME_DURATION);
    }
#endif

    if (bap_frame_duration == BAP_SUPPORTED_FRAME_DURATION_10MS)
    {
        frame_duration = 10000;
    }
    else if (bap_frame_duration == BAP_SUPPORTED_FRAME_DURATION_7P5MS)
    {
        frame_duration = 7500;
    }
    else
    {
        DEBUG_LOG_INFO("leAudioClient_GetFrameDuration Unsupported ");
        Panic();
    }

    return frame_duration;
}

uint16 leAudioClient_GetFrameLength(uint16 max_sdu_size, uint16 stream_type, uint16 codec_type)
{
    uint16 frame_length = max_sdu_size;

    if (stream_type == KYMERA_LE_STREAM_STEREO_USE_BOTH && codec_type == KYMERA_LE_AUDIO_CODEC_LC3)
    {
        frame_length /= 2;
    }

    return frame_length;
}

uint8 leAudioClient_GetCodecType(CapClientAudioConfig cap_client_audio_config)
{
#ifndef INCLUDE_LE_AUDIO_GAMING_MODE_APTX_LITE
    UNUSED(cap_client_audio_config);
#endif

#ifdef INCLUDE_LE_AUDIO_GAMING_MODE_APTX_LITE
    if (cap_client_audio_config.codecId == BAP_CODEC_ID_VENDOR_DEFINED)
    {
        return (cap_client_audio_config.vendorCodecId == CAP_CLIENT_VS_CODEC_ID_APTX_LITE? KYMERA_LE_AUDIO_CODEC_APTX_LITE : KYMERA_LE_AUDIO_CODEC_LC3);
    }
    else
#endif
    {
        return KYMERA_LE_AUDIO_CODEC_LC3;
    }
}
#endif /* defined(INCLUDE_LE_AUDIO_UNICAST_SOURCE) || defined(INCLUDE_LE_AUDIO_BROADCAST_SOURCE) */
